package com.lcz.authority.common.domain;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.lcz.authority.common.domain.entity.SysDeptEntity;
import com.lcz.authority.common.domain.entity.SysMenuEntity;
import com.lcz.authority.common.domain.entity.SysResourceEntity;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lcz
 * @desc Treeselect树结构实体类
 * @mail lcz_0130@163.com
 * @date 2024/05/06
 */
@Data
public class TreeSelect implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 节点ID
     */
    private String id;

    /**
     * 节点名称
     */
    private String label;

    /**
     * 子节点
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<TreeSelect> children;

    public TreeSelect() {
    }

    public TreeSelect(String id, String label) {
        this.id = id;
        this.label = label;
    }

    public TreeSelect(SysDeptEntity dept) {
        this.id = String.valueOf(dept.getDeptId());
        this.label = dept.getDeptName();
        this.children = dept.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    public TreeSelect(String id, String label, List<SysResourceEntity> resources) {
        this.id = id;
        this.label = label;
        this.children = resources.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    public TreeSelect(SysMenuEntity menu) {
        this.id = menu.getMenuId();
        this.label = menu.getMenuName();
        this.children = menu.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    public TreeSelect(SysResourceEntity resource) {
        this.id = resource.getResourceId();
        this.label = resource.getResourceName();
    }

}
